package crawler.naive;

import java.io.File;

import utility.connection.HttpConnector;
import utility.connection.HttpParameters;
import utility.files.TextFile;
import crawler.StatisticsFileWriter;

public class TrovaCasaNaiveCrawler {
	
	private long _maxIndex;
	private long _minIndex;
	public TrovaCasaNaiveCrawler(long maxIndex){
		this(0,maxIndex);
	}
	
	public TrovaCasaNaiveCrawler(long minIndex, long maxIndex){
		if(minIndex<0) minIndex*=-1; //Always positive values
		if(maxIndex<0) maxIndex*=-1; //Always positive values
		
		//Checking intervals coherence 
		if(maxIndex<minIndex){
			long temp = maxIndex;
			maxIndex = minIndex;
			minIndex = temp;
		}
		_minIndex = minIndex;
		_maxIndex = maxIndex;
	}
	
	public void putResultsOn(File path){
		if(!path.isDirectory()) path = new File(path.getParent());
		
		HttpConnector connector = new HttpConnector();
		HttpParameters parameters = new HttpParameters();
		String prefixurl = "http://www.trova-casa.net/annunci/TC-";
		for(long i=_minIndex; i<=_maxIndex; i++){
			String url = prefixurl+i+".htm";
			String fileName = "TC-"+i+".htm";
			TextFile htmlFile = TextFile.inRewrite(path+"/"+fileName);
			TextFile linksFile = TextFile.inAppend(path+"/"+"_links.txt");
			String result = connector.doGet(url, parameters);
			if(!result.trim().isEmpty()&&!result.contains("<title>Trova-Casa.net - Annuncio eliminato</title>")){
				htmlFile.putLine(result);
				linksFile.putLine(url);
				StatisticsFileWriter.getInstance().add("http://www.trova-casa.net/",1);
			}
		}
	}

}
